<?php

namespace common\models\cms;

use Yii;
use common\helpers\Url;
use common\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%cms_article_comment}}".
 *
 * @property int $id
 * @property int $user_id 用户ID
 * @property int $article_id 文章ID
 * @property int $comment_id 评论ID
 * @property int $reply_id 回复ID
 * @property string $post 评论内容
 * @property string $attach 评论附件
 * @property string $author 作者
 * @property string $listener 被回复人
 * @property int $likes 点赞数
 * @property int $status 评论状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class CmsArticleComment extends \common\db\ActiveRecord {

    use \common\traits\relation\UserData;

    /**
     * 状态：0未审核，10已审核
     */
    CONST STATUS_DEFAULT = 0;
    CONST STATUS_ACTIVE = 10;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%cms_article_comment}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['user_id', 'article_id', 'comment_id', 'reply_id', 'likes', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['post'], 'required'],
            [['attach'], 'string'],
            [['post'], 'string', 'max' => 1000],
            [['author', 'listener'], 'string', 'max' => 30],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => '用户ID',
            'article_id' => '文章ID',
            'comment_id' => '评论ID',
            'reply_id' => '回复ID',
            'post' => '评论内容',
            'attach' => '评论附件',
            'author' => '作者',
            'listener' => '被回复人',
            'likes' => '点赞数',
            'status' => '评论状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    /**
     * 关联文章
     */
    public function getArticle() {
        return $this->hasOne(CmsArticle::className(), ['id' => 'article_id']);
    }

    /**
     * 管理上级评论信息
     */
    public function getParent() {
        return $this->hasOne(static::className(), ['id' => 'comment_id']);
    }

    /**
     * 关联回复信息
     */
    public function getChildren() {
        return $this->hasMany(CmsArticleCommentLowest::className(), ['comment_id' => 'id']);
    }

    /**
     * 关联回复信息
     */
    public function getGallery() {
        return $this->hasMany(CmsArticleGallery::className(), ['comment_id' => 'id']);
    }

    /**
     * 关联回复信息
     */
    public function getImages() {
        return ArrayHelper::getColumn($this->gallery, 'source', []);
    }

    /**
     * 关联点赞日志
     */
    public function getLikeLogs() {
        return $this->hasMany(CmsArticleLike::className(), ['comment_id' => 'id']);
    }

    /**
     * 检测当前登录用户是否点赞
     */
    public function getLiked() {
        return $this->getLikeLogs()->andWhere([
            'user_id' => Yii::$app->user->id
        ])->exists();
    }

    /**
     * 获取回复数量
     */
    public function getReplies() {
        return (int) $this->getChildren()->count();
    }

    /**
     * 读取两条评论内容
     */
    public function getComments() {
        return $this->getChildren()->limit(2);
    }

    /**
     * 评论/回复的时间格式化
     * @return string 格式化后的日期
     */
    public function getPublishTime() {
        return Yii::$app->formatter->asRelativeTime($this->created_at);
    }

    public function getLink() {
        return Url::weblink('article.comment', ['id' => $this->id]);
    }

    public function getAuthorName() {
        if ($this->author) {
            return $this->author;
        }
        if ($this->userCell) {
            return $this->userCell->nickname;
        }
        return '外星人';
    }

    /**
     * 接口返回基础数据结构
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id',
            'user' => 'userCell',
            'content' => 'post',
            'author' => 'authorName',
            'listener',
            'article_id',
            'comment_id',
            'comments',
            'replies',
            'likes',
            'liked',
            'publishTime',
            'link',
            'images',
        ];
    }

}
